import 'package:flutter/widgets.dart';
import 'package:my_flutter/app.dart';
import 'package:my_flutter/pages/LoginPage.dart';
import 'package:my_flutter/pages/HomePage.dart';
import 'package:my_flutter/pages/NoFoundPage.dart';
import 'package:my_flutter/pages/PrintPage.dart';
import 'package:my_flutter/pages/WebViewPage.dart';

///@ClassName: Routers
/// @Author: guoyangyang
///@Date: 2022 3/9/22 2:45 PM
///@Description: 路由管理文件

class Routers {

  //打开路由
  static push(String name, [Object? arguments]) {
    return App.navigatorKey.currentState?.pushNamed(name, arguments: arguments);
  }

  //关闭路由
  static pop([Object? arguments]) {
    App.navigatorKey.currentState?.pop(arguments);
  }

  static Route<dynamic>? onGenerateRoute(RouteSettings settings) {
    //本以为这里会传进来route.path，但实际是route，所以这里再次获取一下 route.path
    // debugPrint(settings.name); // /loginPage?name=100
    var path = settings.name?.split("?")[0];

    switch(path){
      case RouteName.initPage:
        return NoAnimRouteBuilder(const PrintPage());
      case RouteName.homePage:
        return NoAnimRouteBuilder(const HomePage());
      case RouteName.webViewPage:
        return NoAnimRouteBuilder(WebViewPage(settings.arguments));
      case RouteName.printPage:
        return NoAnimRouteBuilder(const PrintPage());
      default:
        return NoAnimRouteBuilder(const NoFoundPage());
    }
  }

}

class RouteName {
  //初始路由，LoginPage
  static const String initPage = '/';
  //404路由，NoFoundPage
  static const String noFoundPage = '/noFoundPage';
  //HomePage 路由
  static const String homePage = '/homePage';
//webView页面 路由
  static const String webViewPage = '/webViewPage';
  //printPage页面 路由
  static const String printPage = '/printPage';

}

class NoAnimRouteBuilder extends PageRouteBuilder {
  final Widget page;

  NoAnimRouteBuilder(this.page)
      : super(
      opaque: false,
      pageBuilder: (context, animation, secondaryAnimation) => page,
      transitionDuration: Duration(milliseconds: 0),
      transitionsBuilder:
          (context, animation, secondaryAnimation, child) => child);
}
